package com.cm.order.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cm.order.entity.OrderGoods;
import com.cm.order.entity.Orders;
import com.cm.order.entity.dto.OrdersKeyWordDTO;
import com.cm.order.feign.GoodsFeign;
import com.cm.order.mapper.OrderGoodsMapper;
import com.cm.order.mapper.OrdersMapper;
import com.cm.order.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @类 名:OrdersServiceImpl </br>
 * @描 述:<br>
 * @日 期:2020/9/24 18:22 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Autowired
    private GoodsFeign goodsFeign;
    @Override
    public boolean insert(Orders orders) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_id",orders.getOrderId());
        List<Orders> brands = ordersMapper.selectList(queryWrapper);
        if(brands.size() > 0){
            return false;
        }
        ordersMapper.insert(orders);
        return true;
    }

    @Override
    public boolean delete(Long orderId) {
        if(ordersMapper.deleteById(orderId) > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Orders orders) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_id",orders.getOrderId());
        if (ordersMapper.update(orders,queryWrapper) > 0){
            return true;
        }
        return false;
    }

    @Override
    public IPage<Orders> getOrderList(OrdersKeyWordDTO ordersKeyWordDTO) {
        Page<Orders> page = new Page<>();
        ordersKeyWordDTO.setCurrentPage((ordersKeyWordDTO.getCurrentPage()-1)*ordersKeyWordDTO.getPageSize());
        List<Orders> adminList=ordersMapper.getOrdersList(ordersKeyWordDTO);
        page.setRecords(adminList);
        page.setCurrent(ordersKeyWordDTO.getCurrentPage());
        page.setSize(ordersKeyWordDTO.getPageSize());
        int count=ordersMapper.getOrdersTotal(ordersKeyWordDTO);
        page.setTotal(count);
        return page;
    }

    @Override
    public List<Orders> getOrdersList(Long userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        List<Orders> ordersList=ordersMapper.selectList(queryWrapper);
        for (Orders orders:ordersList){
            queryWrapper=new QueryWrapper();
            queryWrapper.eq("order_id",orders.getOrderId());
            List<OrderGoods> orderGoods= orderGoodsMapper.selectList(queryWrapper);
            for (OrderGoods orderGoods1:orderGoods){
                Map<String,Object> good=(Map<String,Object>) goodsFeign.getGoodsById(orderGoods1.getGoodsId()).getData();
                orderGoods1.setGood(good);
            }
            orders.setOrderGoods(orderGoods);
        }
        return ordersList;
    }
}
